home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cc04.arc / SIEVE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-03-15  |  884 b   |  41 lines

  1. /* -- sieve.c  Eratosthenes Seive Prime Number Program -- */
  2.  
  3. #include "stdio.h"
  4.  
  5. #define TRUE 1
  6. #define FALSE 0
  7. #define SIZE 8190
  8. #define NTIMES 10
  9.  
  10.     char flags[SIZE+1];
  11.  
  12. main()                /* compute primes using sieve of Eratosthenes */
  13. {
  14.  
  15.     int i,prime,k,count,iter;
  16.  
  17.     printf("10 iterations\n");
  18.     for (iter = 1; iter <= NTIMES; iter++) {    /* do program 10 times */
  19.         count = 0;            /* prime counter */
  20.         for (i = 0; i <= SIZE; flags[i++] = TRUE);    /* set all flags true */
  21.         for (i = 0; i <= SIZE; i++) {
  22.             if (flags[i]) {     /* found a prime */
  23.                 prime = i + i + 3;    /* twice index + 3 */
  24. /*                printf("\n%d", prime);  */
  25.                 for (k = i + prime; k <= SIZE; flags[k += prime] = FALSE);    /* kill all multiples */
  26.                 count ++;    /* primes found */
  27.             }
  28.         }
  29.     }
  30.     printf("\n%d primes.", count);          /* primes found on tenth pass */
  31.     exit(0);
  32. }
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.